package com.misoo.ex05;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class Ex05Activity extends Activity implements OnItemClickListener{
	public static final int RELOAD_ID = Menu.FIRST;
	public static final int EXIT_ID = Menu.FIRST + 1;
	ListView lv ;
	ArrayAdapter<String> adapter;
	DataModel dm;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        dm = new DataModel();
        lv = new ListView(this);
        setContentView(R.layout.main);
        
        adapter = new ArrayAdapter<String>
        	(this, android.R.layout.simple_list_item_1,dm.loadData());
        
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);
        setContentView(lv);
    }

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		setTitle("Choise:" + String.valueOf(dm.getPrice(arg2)));
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		menu.add(0,RELOAD_ID, 0, "Reload");
		MenuItem im = menu.findItem(RELOAD_ID);
		im.setIcon(R.drawable.reload_im);
		menu.add(0,EXIT_ID,1,"Exit");
		im = menu.findItem(EXIT_ID);
		im.setIcon(R.drawable.exit_im);
		return true;
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
		case RELOAD_ID:
			adapter = new ArrayAdapter<String>
				(this, android.R.layout.simple_list_item_1,dm.loadData());
			lv.setAdapter(adapter);
			break;
		case EXIT_ID:
			finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
    
    
}